package com.tensquare.recruit.controller;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.recruit.pojo.Recruit;
import com.tensquare.recruit.service.RecruitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("recruit")
@CrossOrigin
public class RecruitController {
    @Autowired
    private RecruitService recruitService;
    @PostMapping
    public Result save(@RequestBody Recruit recruit){
        recruitService.save(recruit);
        return new Result(true, StatusCode.OK,"ok");
    }
    //  GET/recruit 标签全部列表
    @GetMapping
    public Result findAll(){
        List<Recruit> recruits=recruitService.findAll();
        return new Result(true,StatusCode.OK,"ok",recruits);
    }
    // GET/recruit/{recruitId}根据ID查询
    @GetMapping("/{recruitId}")
    public Result findByRecruitId(@PathVariable String recruitId) {
        Recruit recruit=recruitService.findById(recruitId);
        return new Result(true,StatusCode.OK,"ok",recruit);
    }
    // PUT/recruit/{recruitId}修改标签
    @PutMapping("{recruitId}")
    public Result findByRecruitId(@PathVariable String recruitId,@RequestBody Recruit recruit) {
        recruit.setId(recruitId);
        recruitService.updateByRecruitId(recruit);
        return new Result(true,StatusCode.OK,"ok");
    }
    //  DELETE/recruit/{recruitId}根据ID删除
    @DeleteMapping("{recruitId}")
    public  Result deleteByRecruitId(@PathVariable String recruitId){
        recruitService.deleteByRecruitId(recruitId);
        return new Result(true,StatusCode.OK,"ok");
    }
    /*
    * POST
    /recruit/search/{page}/{size}
    标签分页*/
    @PostMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map
    ){
        Page<Recruit> pageData=recruitService.findByPage(map,page,size);
        PageResult<Recruit> pageResult=new PageResult<>(pageData.getTotal(),pageData.getRecords());
        return new Result(true, StatusCode.OK,"ok",pageResult);
    }
    /*
    * POST
    /recruit/search
    条件查询*/
    @PostMapping("/search")
    public Result findBySearch( @RequestBody Map<String,Object> map){
        List<Recruit> list=recruitService.findBySearch(map);
        return new Result(true,StatusCode.OK,"ok",list);
    }
    /*
    * GET
    /recruit/search/recommend
    * 查state=2且按日期降序排前4个
    推荐职位*/
    @GetMapping("/search/recommend")
    public Result recommend() {
       return new Result(true,StatusCode.OK,"ok",recruitService.recommend());
    }


    /*
    GET
/recruit/search/newlist
最新职位
查state=1且按日期降序排前12个
    * */
    @GetMapping("/search/newlist")
    public Result newlist() {
        return new Result(true,StatusCode.OK,"ok",recruitService.newlist());
    }
}
